﻿
using System ;

using MskEdit.Dxf.Model ;


namespace MskEdit.Dxf.Backend
{

    public class DxfValueReader
    {        
        internal static object TryRead( string[] allLines, ref int lineNo, object fallbackValue ) {
        
            int internalLineNo = lineNo ;
            
            DxfCode dxfCode = DxfCodeReader.TryRead( allLines, ref lineNo, null ) ;
            
            if ( dxfCode == null )
                return fallbackValue ; 

            if ( dxfCode.HasStringCode ) {
                string stringValue = DxfStringReader.TryReadAfterCode( allLines, ref lineNo, null ) ;
                if ( stringValue != null )
                    return stringValue ;
            }
            
            if ( dxfCode.HasDouble3dPointCode ) {
                DxfDouble3dPoint pointValue = DxfDouble3dPointReader.TryReadAfterCode( dxfCode, allLines, ref lineNo, null ) ;
                if ( pointValue != null )
                    return pointValue ;
            }

            if ( dxfCode.HasDoubleCode ) {
                double? doubleValue = DxfDoubleReader.TryReadAfterCode( allLines, ref lineNo, null ) ;
                if ( doubleValue != null )
                    return doubleValue ;
            }
            
            if ( dxfCode.HasShortCode ) {
                short? shortValue = DxfShortReader.TryReadAfterCode( allLines, ref lineNo, null ) ;
                if ( shortValue != null )
                    return shortValue ;
            }
            
            if ( dxfCode.HasIntCode ) {
                int? intValue = DxfIntReader.TryReadAfterCode( allLines, ref lineNo, null ) ;
                if ( intValue != null )
                    return intValue ;
            }

            if ( dxfCode.HasLongCode ) {
                long? longValue = DxfLongReader.TryReadAfterCode( allLines, ref lineNo, null ) ;
                if ( longValue != null )
                    return longValue ;
            }
            
            bool? boolValue = DxfBoolReader.TryReadAfterCode( allLines, ref lineNo, null ) ;
            if ( boolValue != null )
                return boolValue ;
        
            lineNo = internalLineNo ;
            
            return fallbackValue ;

        }        
    }
}
